<!-- 账号添加 -->
<template>
  <h2 style="color: #595959; margin-bottom: 20px">
    {{ $t("messages.ProductAdd") }}
  </h2>
  <el-form label-width="100px">
    <el-form-item label="账号">
      <el-input v-model="form.account" style="width: 400px"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input
        type="password"
        v-model="form.password"
        style="width: 400px"
      ></el-input>
    </el-form-item>
    <el-form-item label="用户组">
      <el-select
        v-model="form.userGroup"
        style="width: 400px"
        placeholder="请选择用户组"
      >
        <el-option
          v-for="item in userGroups"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleAdd">添加</el-button>
      <el-button @click="handleReset">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { addUser } from "@/api/index.js";

const form = ref({
  account: "",
  password: "",
  userGroup: "",
});

// 假设的用户组数据，实际应用中可能需要从后端获取
const userGroups = ref([
  { value: "超级管理员", label: "超级管理员" },
  { value: "普通管理员", label: "普通管理员" },
]);

const handleAdd = async () => {
  try {
    // 调用 addUser 函数发送请求
    const res = await addUser(form.value);
    console.log("🚀 ~ handleAdd ~ res:", res);
    if (res.code === 0) {
      // 如果响应码为 0，提示添加成功
      ElMessage.success("添加成功");
    } else {
      // 其他响应码提示添加失败
      ElMessage.error("添加失败");
    }
  } catch (error) {
    // 捕获请求错误
    ElMessage.error("添加账号请求异常");
    console.error(error);
  }
};

const handleReset = () => {
  form.value = {
    username: "",
    password: "",
    userGroup: "",
  };
  // 重置表单
};
</script>

<style></style>
